﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CONTROLLER;
using ENTITY;
using DevExpress.XtraEditors;

namespace frm_view
{
    public partial class frmBai : Form
    {
        bool IsInsert = false;
        void KhoaDieuKhien()
        {
            txtMaBai.Enabled = false;
            txtNoiDung.Enabled = false;
            txtTenBai.Enabled = false;
            txtVideo.Enabled = false;
            cbbChuong.Enabled = false;

            btnSave.Enabled = false;
            btnDelete.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
        }

        void MoDieuKhien()
        {
            txtMaBai.Enabled = true;
            txtNoiDung.Enabled = true;
            txtTenBai.Enabled = true;
            txtVideo.Enabled = true;
            cbbChuong.Enabled = true;

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnInsert.Enabled = false;
            btnUpdate.Enabled = false;
        }
        ctrls_Chuong obj_chuong = new ctrls_Chuong();
        void Refesh()
        {
            cbbChuong.Properties.DataSource = obj_chuong.GetData_SelectSTR("Select MaChuong,TenChuong From tblChuong Where MaChuong = MaChuong");
            cbbChuong.Properties.DisplayMember = "MaChuong";
            cbbChuong.Properties.ValueMember = "TenChuong";
            msds.DataSource =  obj_bai.Bai_SelectALL();
        }
        public frmBai()
        {
            InitializeComponent();
        }
        ctrls_Bai obj_bai = new ctrls_Bai();
        EN_Bai obj_en_bai = new EN_Bai();
        private void ViewBai_Load(object sender, EventArgs e)
        {
            Refesh();
            KhoaDieuKhien();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            IsInsert = true;
            MoDieuKhien();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            IsInsert = false;
            MoDieuKhien();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn đồng ý xóa ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                try
                {
                    obj_en_bai.MaBai = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                    obj_bai.Bai_Delete(obj_en_bai);
                    XtraMessageBox.Show("Đã xóa thành công","Thông báo");
                    Refesh();
                }
                catch
                {

                    MessageBox.Show("Vui lòng xóa các \"MỤC\" tồn tại trong \"BÀI\"", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void msds_Click(object sender, EventArgs e)
        {
            try
            {
                cbbChuong.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]).ToString();
                txtMaBai.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                txtTenBai.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2]).ToString();
                txtNoiDung.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[3]).ToString();
            }
            catch
            {

                return;
            }
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Text File (*.rtf)|*.rtf";
            if (ofd.ShowDialog() == DialogResult.OK)
                txtNoiDung.Text = @"\" + ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1); ;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Error.ClearErrors();


            obj_en_bai.MaChuong = cbbChuong.Text;
            obj_en_bai.MaBai = txtMaBai.Text;
            obj_en_bai.TenBai = txtTenBai.Text;
            obj_en_bai.NoiDug = txtNoiDung.Text;
            obj_en_bai.Video = txtVideo.Text;
            if (string.IsNullOrEmpty(cbbChuong.Text))
                Error.SetError(cbbChuong, "Lựa chọn \"CHƯƠNG\"");
            else if (string.IsNullOrEmpty(txtMaBai.Text))
                Error.SetError(txtMaBai, "Nhập thông tin \"MÃ BÀI\"");
            else if (string.IsNullOrEmpty(txtTenBai.Text))
                Error.SetError(txtTenBai, "Nhập thông tin \"TÊN BÀI\"");
            else if (string.IsNullOrEmpty(txtNoiDung.Text))
                Error.SetError(txtNoiDung, "Nhập thông tin \"NỘI DUNG\"");


            else if (IsInsert == true)
            {

                obj_bai.Bai_Insert(obj_en_bai);
                XtraMessageBox.Show("Thêm thành công", "Thông báo");
                Refesh();
                KhoaDieuKhien();
            }
            else
            {
                obj_bai.Bai_Update(obj_en_bai);
                XtraMessageBox.Show("Cập nhật thành công", "Thông báo");
                Refesh();
                KhoaDieuKhien();
            }
        }
        public event EventHandler closeBai;
        private void frmBai_FormClosing(object sender, FormClosingEventArgs e)
        {
            closeBai(this, new EventArgs());
        }

        private void btnVideo_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                txtVideo.Text = @"\" + ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1);
        }
    }
}
